﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace Slaks.Graphs.Graphs.Slak
{
    class SlakGraph : Graph
    {
        public override Graph GetMCS(Graph two)
        {
            Graph result = base.GetMCS(two);

            #region find additiona edges

            VertexList resultList = result.GetVertexList();
            if (resultList.Count <= 1) return result;

            Vertex vertexOne;
            Vertex vertexTwo;

//            Vertex vertexOneNext;
            Vertex vertexTwoNext;

            long id;
            long nextId;
            IDictionary sectionsOne, sectionsTwo, sectionsResult;
            IList commonSection = new ArrayList();
            for (int i = 0; i < resultList.Count; i++)
            {
                id = resultList[i].GetId();
                vertexOne = this.GetVertex(id);
                vertexTwo = two.GetVertex(id);

                for (int j = i + 1; j < resultList.Count; j++)
                {
                    nextId = resultList[j].GetId();

                    #region

                    /*
					sectionsOne = vertexOne.GetSectionsIfConnected(nextId);
					sectionsTwo = vertexTwo.GetSectionsIfConnected(nextId);

					ICollection sectionsOneKeys = sectionsOne.Keys;

					foreach (string section in sectionsOneKeys)
					{
						if (sectionsTwo.Contains(section))
						{
							resultList[i].AddEdge(resultList[j], section);
						}
					}

					if (id == nextId) continue;
					vertexOneNext = one.GetVertex(nextId);
					vertexTwoNext = two.GetVertex(nextId);

					sectionsOne = vertexOneNext.GetSectionsIfConnected(id);
					sectionsTwo = vertexTwoNext.GetSectionsIfConnected(id);

					sectionsOneKeys = sectionsOne.Keys;
					foreach (string section in sectionsOneKeys)
					{
						if (sectionsTwo.Contains(section))
						{
							resultList[j].AddEdge(resultList[i], section);
						}
					}*/

                    #endregion

                    sectionsOne = vertexOne.GetSectionsIfConnected(nextId);

                    vertexTwoNext = two.GetVertex(nextId);
                    sectionsTwo = vertexTwoNext.GetSectionsIfConnected(id);


                    foreach (string section1 in sectionsOne.Keys)
                    {
                        if (sectionsTwo.Contains(section1) == true) commonSection.Add(section1);
                    }

                    sectionsResult = resultList[i].GetSectionsIfConnected(nextId);

                    //add connection to result if edge with connection does not exists
                    foreach (string section in commonSection)
                    {
                        if (sectionsResult.Contains(section) == false) resultList[i].AddEdge(resultList[j], section);
                    }

                    commonSection.Clear();

                } //end of for
            }

            #endregion

            return result;
        }
    }
}
